﻿using HslCommunication;
using HslCommunication.Profinet.Siemens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace paraInterface
{
    public class Siemens300PLC_B:PLC//环形炉
    {
        public SiemensS7Net SiemensPLC { get; set; }
        Dictionary<string, string> dict = new Dictionary<string, string>();
        public bool isConnected = false;
        public override bool InitPLC()
        {
            if (IsIP(IP))
            {
                SiemensPLC = new SiemensS7Net(SiemensPLCS.S300, IP);
                SiemensPLC.ConnectTimeOut = 5000;
                isRealPLC=true;
                for (int i = 0; i < DATALENTH; i++)
                {
                    dict.Add("H" + i.ToString().PadLeft(3, '0'), "0");
                }
            }
            return isRealPLC;
        }

        public override bool SendData2PLC(jsonData data)//需要将数据进行解析
        {
            int res = 0;
            if (Connect2PLC())
            {
                try
                {
                    //将数据进行解析发送到PLC
                    //梳理数据
                    short[] v1 = new short[50];

                    for (int i = 0; i < data.PARAMS.Length && i < DATALENTH; i++)
                    {
                        ParameterData parameterData= data.PARAMS.ToList().Find(x => x.TBGD_ITEM_CODE == "H" + (i + 1).ToString().PadLeft(3, '0'));
                        if (parameterData == null) continue;
                        if (parameterData.TBCD_UNIT == "%")//碳势
                        {
                            if (float.TryParse(parameterData.TBGD_UPPER_LIMIT, out float x))
                                v1[i] = (short)(x*100);
                        }
                        else
                        {
                            if (short.TryParse(parameterData.TBGD_UPPER_LIMIT, out short x))
                                v1[i] = x;
                        }
                        //if (Convert.ToInt32(data.PARAMS[i].TBGD_ITEM_CODE.Substring(1)) != (i + 1))
                        //    return false;
                        //dict[data.PARAMS[i].TBGD_ITEM_CODE] = data.PARAMS[i].TBGD_UPPER_LIMIT;
                    }

                    //for (int i = 0; i < v1.Length; i++)
                    //{
                    //    v1[i] = Convert.ToInt16("0" + dict["H" + (i+1).ToString().PadLeft(3,'0')]);
                    //}
                    //short[] v2 = new short[31];
                    //for (int i = 0; i < v2.Length; i++)
                    //{
                    //    v2[i] = Convert.ToInt16("0" + dict["H" + (i + 9)]);
                    //}
                    //short[] v3 = new short[9];
                    //for (int i = 0; i < v3.Length; i++)
                    //{
                    //    v3[i] = Convert.ToInt16("0" + dict["H"+ (i + 40)]);
                    //}
                    //res += SiemensPLC.Write("DB360.2.0", false).IsSuccess ? 0 : 1;//远程下载工艺时需置零DB360.DBX2.0
                    Thread.Sleep(200);
                    //res += SiemensPLC.Write(ADD + ".20", v1).IsSuccess ? 0 : 1;
                    //res += SiemensPLC.Write(ADD + ".60", v2).IsSuccess ? 0 : 1;
                    //res += SiemensPLC.Write(ADD + ".140", v3).IsSuccess ? 0 : 1;
                    v1[49] = 1;
                    res += SiemensPLC.Write(ADD + ".0", v1).IsSuccess ? 0 : 1;
                    //res += SiemensPLC.Write(ADD + ".18", v2).IsSuccess ? 0 : 1;
                    //res += SiemensPLC.Write(ADD + ".80", v3).IsSuccess ? 0 : 1;

                }
                catch (Exception)
                {
                    res = 1000;
                }
            }
            else
            {
                res = 999;
            }
            SiemensPLC.ConnectClose();
            return (res == 0 ? true : false);
        }

        private bool Connect2PLC()
        {
            if (SiemensPLC == null) return false;
            try
            {
                OperateResult res = SiemensPLC.ConnectServer();
                isConnected = res.IsSuccess;
            }
            catch (Exception)
            {
                isConnected = false;
            }
            if (!isConnected)
                pubcls.WriteLog(IP + "的PLC连接失败！请检查网络或PLC！");
            return isConnected;
        }
    }
}
